﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace game_for_test_helper.src.util
{
    public static class SerializeUtil
    {
        public static void SerilizeAnObject(object obj, string path) //序列化
        {
            System.IO.FileStream stream = new FileStream(path, FileMode.Create);
            try
            {
                System.Xml.Serialization.XmlSerializer serializer =
                    new System.Xml.Serialization.XmlSerializer(obj.GetType());
                serializer.Serialize(stream, obj);
            }
            catch (Exception ex)
            {
                Console.WriteLine("SerilizeAnObject Exception: {0}", ex.Message);
            }
            finally
            {
                stream.Close();
                stream.Dispose();
            }
        }
        public static Object DeSerilizeAnFileToObject(string file, Type type)
        {
            FileStream stream = null;
            Object obj = null;
            XmlSerializer serializer = new XmlSerializer(type);
            try
            {
                stream = new FileStream(file, FileMode.Open, FileAccess.Read);
                obj = serializer.Deserialize(stream);
            }
            catch (Exception ex)
            {
                Console.WriteLine("SerilizeAnObject Exception: {0}", ex.Message);
                Console.WriteLine("SerilizeAnObject Exception: {0}", ex.ToString());
                Console.WriteLine("SerilizeAnObject Exception: {0}", ex.StackTrace);
                
            }
            finally
            {
                stream.Close();
                stream.Dispose();
            }
            return obj;
        } 
    }
}
